package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.AdminInfo;


import com.cskaoyan.realm.MallToken;
import com.cskaoyan.service.AdminService;
import com.cskaoyan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Map;

@Controller
@RequestMapping("admin/auth")
public class AuthController {
    @Autowired
    AdminService adminService;

    @PostMapping("login")
    public String login(@RequestBody Map map, HttpServletRequest request) {
        String username = (String) map.get("username");
        String password = (String) map.get("password");
        MallToken adminToken = new MallToken(username, password, "admin");

        //登录的具体业务 → shiro整合的时候
        Subject subject = SecurityUtils.getSubject();
        //TODO 用户名异常会报空指针 接收不到
        try {
            subject.login(adminToken);
            //登录成功之后将数据插入日志
            String ip = request.getRemoteAddr();
            String action = "登录";
            adminService.insertlog(username,ip,action);

        } catch (IncorrectCredentialsException | NullPointerException e) {
            return "forward:/admin/auth/fail";
        }catch (AuthenticationException e) {
            return "forward:/admin/auth/fail";
        }
        return "forward:/admin/auth/success";
    }


    @RequestMapping("success")
    @ResponseBody
    public BaseRespVo success(){
        Subject subject = SecurityUtils.getSubject();
        Serializable sessionId = subject.getSession().getId();
        String id = sessionId.toString();
        return BaseRespVo.ok(id);
    }
    @RequestMapping("fail")
    @ResponseBody
    public BaseRespVo fail(){
        return BaseRespVo.fail605();
    }
 @RequestMapping("please")
    @ResponseBody
    public BaseRespVo please(){
        return BaseRespVo.fail501();
    }

    @RequestMapping("info")
    @ResponseBody
    public BaseRespVo info(String token){
        Subject subject = SecurityUtils.getSubject();
        String username = (String) subject.getPrincipal();
        //根据username查询对应的信息
        AdminInfo adminInfo = adminService.selectAdminInfo(username);


        return BaseRespVo.ok(adminInfo);
    }

    @RequestMapping("logout")
    @ResponseBody
    public BaseRespVo logout(HttpServletRequest request){
        Subject subject = SecurityUtils.getSubject();
        String username = (String)subject.getPrincipal();
        String ip = request.getRemoteAddr();
        String action = "退出";
        adminService.insertlog(username,ip,action);
        subject.logout();
        return BaseRespVo.ok();
    }
}
